package com.like.lazyfragment.ui2

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import com.like.lazyfragment.R

open abstract class LazyFragment : Fragment() {
    private var textView: TextView? = null
    private var inflates: View? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        if (inflates == null) {
            inflates= inflater.inflate(getLayout(), container, false)
        }
        textView = inflates?.findViewById<TextView>(R.id.textView)
        if (userVisibleHint) {
            textView?.text = getString()
        }
        return inflates
    }

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        if (isVisibleToUser) {
            textView?.text = getString()
        } else textView?.text = "Loading.................."
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
    }

    override fun onResume() {
        super.onResume()
    }

    override fun onPause() {
        super.onPause()
    }

    abstract fun getLayout(): Int
    abstract fun getString(): String
}